#include "MyNewSoundWin.h"


void MyNewSoundWin::windowSize()
{
	setMinimumSize(MyNewSoundWin::winWidth, MyNewSoundWin::winHeight);
	resize(MyNewSoundWin::winWidth, MyNewSoundWin::winHeight);
	setWindowTitle(tr("Create a new sound"));
}


MyNewSoundWin::MyNewSoundWin(SoundTab * st, QMainWindow *parent, SoundObj **soundO):QDialog(parent)
{
	so = soundO;
	windowSize();

	this->soundTab = st;

	QVBoxLayout *mainLayout = new QVBoxLayout;

	signalFormLabel  = new QLabel(tr("Signal form: "));
	signalFreqLabel  = new QLabel(tr("Signal frequency: "));
	soundLengthLabel = new QLabel(tr("Duration: "));
	amplitudeLabel   = new QLabel(tr("Amplitude:"));
	sampleFreqLabel  = new QLabel(tr("Sampling frequency: "));

	signalFormCombo = new QComboBox();
	signalFormCombo->addItem(tr("Sinusoidal"));
	signalFormCombo->addItem(tr("Square"));
	signalFormCombo->addItem(tr("Triangular"));
	signalFormCombo->addItem(tr("Positive sinusoidal"));

	signalFreqLine = new QSpinBox();
	signalFreqLine->setRange(1, INFINITY);
	signalFreqLine->setSuffix(" Hz");

	soundLengthLine = new QSpinBox();
	soundLengthLine->setRange(1, INFINITY);
	soundLengthLine->setSuffix(" s");

	amplitudeSlider = new QSlider(Qt::Horizontal);
	amplitudeSlider->setRange(0, 32767);

	sampleFreqLine  = new QSpinBox();
	sampleFreqLine->setRange(1, INFINITY);
	sampleFreqLine->setSuffix(" Hz");

	amplitudeValue = new QLabel(QString::number(amplitudeSlider->value()));

	connect(amplitudeSlider, SIGNAL(valueChanged(int)), amplitudeValue, SLOT(setNum(int)));

	warning = new QLabel;
	warning->setAlignment(Qt::AlignCenter);
	connect(signalFreqLine, SIGNAL(valueChanged(int)), this, SLOT(checkFrequency()));
	connect(sampleFreqLine, SIGNAL(valueChanged(int)), this, SLOT(checkFrequency()));

	QHBoxLayout *layout = new QHBoxLayout;
	layout->addWidget(amplitudeLabel);
	layout->addWidget(amplitudeValue);

	mainLayout->addWidget(signalFormLabel);
	mainLayout->addWidget(signalFormCombo);
	mainLayout->addWidget(signalFreqLabel);
	mainLayout->addWidget(signalFreqLine);
	mainLayout->addWidget(soundLengthLabel);
	mainLayout->addWidget(soundLengthLine);
	mainLayout->addLayout(layout);
	mainLayout->addWidget(amplitudeSlider);
	mainLayout->addWidget(sampleFreqLabel);
	mainLayout->addWidget(sampleFreqLine);
	mainLayout->addWidget(warning);
	mainLayout->addStretch(1);

	buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

	mainLayout->addWidget(buttonBox);

	setLayout(mainLayout);
}
void MyNewSoundWin::accept()
{
	//(0,440,60,30000,44100);

	*so = new SoundObj(
		signalFormCombo->currentIndex(),
		signalFreqLine->value(),
		soundLengthLine->value(),
		amplitudeSlider->value(),
		sampleFreqLine->value()
	);

	this->soundTab->setSoundObj(*so);
	this->soundTab->buildBlocks();
	this->close();
}

void MyNewSoundWin::checkFrequency()
{
	if (sampleFreqLine->value() < 2 * signalFreqLine->value())
	{
		warning->setText(tr("<b><font color='red'>Warning:<br>"
				"The sampling frequency is smaller than<br>"
				"the Nyquist frequency!</font></b>"));
	}
	else
	{
		warning->setText("");
	}
}

MyNewSoundWin::~MyNewSoundWin(){}
